package com.csht.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import com.csht.manager.CompanyManager;
import com.csht.service.CompanyService;
import com.csht.command.GetCompanyCommand;
import com.csht.command.ListCompanyCommand;
import com.csht.command.CompanyBatchCommand;
import com.csht.command.CompanyCommand;
import com.csht.command.CompanyQueryCommand;
import com.csht.vo.CompanyVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import com.winit.pms.spi.v2.common.PmsConstants;

import com.winit.common.query.Page;
import com.winit.common.spi.SPIException;
import com.winit.pms.spi.v2.common.PageVo;

/**
 * 
 * 运输企业service服务类
 * 
 * @version 
 * <pre>
 * Author	Version		Date		Changes
 * houzw    1.0  2018年01月05日 Created
 *
 * </pre>
 * @since 1.
 */
@Service("companyService")
public class CompanyServiceImpl implements CompanyService {

    private Logger        logger = LoggerFactory.getLogger(CompanyServiceImpl.class);

    @Resource
    private CompanyManager companyManager;

    @Override
    public CompanyVo getCompany(GetCompanyCommand command) throws SPIException {
        logger.info("单个查询：{}", command);
        Long id = command.getId();
        
        if (id == null) {
            logger.info("id不能为空");
            throw new SPIException("");
        }
        return companyManager.getCompany(id);
    }

    @Override
    public Long createCompany(CompanyCommand command) throws SPIException {
        logger.info("单个新增：{}", command);
        CompanyVo vo = command.getVo();
        
        if (vo == null) {
            logger.info("vo不能为空");
            throw new SPIException("");
        }
        
        vo.setCreated(new Date());
        vo.setCreatedby(command.getCtx().getUsername());
        vo.setOrganizationId(command.getCtx().getOrgId());
        vo.setIsActive(PmsConstants.ACTIVE_Y);
        vo.setIsDelete(PmsConstants.DELETE_N);
        return companyManager.createCompany(vo);
    }

    @Override
    public void createBatchCompany(CompanyBatchCommand command) throws SPIException {
        logger.info("批量新增：{}", command);
        List<CompanyVo> vos = command.getVos();
        
        if (vos == null || vos.size() == 0) {
            logger.info("vos不能为空");
            throw new SPIException("");
        }
        
        for (CompanyVo vo : vos) {
            vo.setCreated(new Date());
            vo.setCreatedby(command.getCtx().getUsername());
            vo.setOrganizationId(command.getCtx().getOrgId());
            vo.setIsActive(PmsConstants.ACTIVE_Y);
            vo.setIsDelete(PmsConstants.DELETE_N);
        }
        companyManager.createBatchCompany(vos);
    }

    @Override
    public void deleteCompany(GetCompanyCommand command) throws SPIException {
        logger.info("单个删除：{}", command);
        Long id = command.getId();
        
        if (id == null) {
            logger.info("id不能为空");
            throw new SPIException("");
        }
        companyManager.deleteCompany(id);
    }

    @Override
    public void deleteBatchCompany(ListCompanyCommand command) throws SPIException {
        logger.info("批量删除：{}", command);
        List<Long> ids = command.getIds();
        
        if (ids == null || ids.size() == 0) {
            logger.info("ids不能为空");
            throw new SPIException("");
        }
        companyManager.deleteBatchCompany(ids);
    }

    @Override
    public void updateCompany(CompanyCommand command) throws SPIException {
        logger.info("单个更新：{}", command);
        CompanyVo vo = command.getVo();
        
        if (vo == null) {
            logger.info("vo不能为空");
            throw new SPIException("");
        }
        
        vo.setUpdated(new Date());
        vo.setUpdatedby(command.getCtx().getUsername());
        companyManager.updateCompany(vo);
    }

    @Override
    public void updateBatchCompany(CompanyBatchCommand command) throws SPIException {
        logger.info("批量更新：{}", command);
        List<CompanyVo> vos = command.getVos();
        
        if (vos == null || vos.size() == 0) {
            logger.info("vos不能为空");
            throw new SPIException("");
        }
        
        for (CompanyVo vo : vos) {
            vo.setUpdated(new Date());
            vo.setUpdatedby(command.getCtx().getUsername());
        }
        companyManager.updateBatchCompany(vos);
    }

    @Override
    public Page<CompanyVo> findCompanyPage(CompanyQueryCommand command) throws SPIException {
        logger.info("分页查询：{}", command);
        CompanyVo vo = command.getVo();
        PageVo pageVo = command.getPageVo();
        return companyManager.findCompanyPage(pageVo, vo);
    }

    @Override
    public List<CompanyVo> queryCompanyList(CompanyQueryCommand command) throws SPIException {
        logger.info("查询所有：{}", command);
        CompanyVo vo = command.getVo();
        return companyManager.queryCompanyList(vo);
    }
}
